﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using jSLCore.Utility.Extensions;

namespace jSLCore.Client.Presentation.Multimedia.Players
{
    /// <summary>
    /// SoundPlayer, can use to play any single file.Has built-in play/stop sound effect
    /// </summary>
    public partial class jMusicPlayer : UserControl
    {
        public jMusicPlayer()
        {
            InitializeComponent();
            if (!DesignerProperties.IsInDesignTool)
            {
                _timer = new Timer(player => UpdatePosition((jMusicPlayer) player), this, new TimeSpan(0, 0, 1),
                                   new TimeSpan(0, 0, 1));
            }

        }

        private Timer _timer;
        private static void UpdatePosition(jMusicPlayer player)
        {

          
            player.Dispatcher.BeginInvoke(() =>
                                              {

                                                  if (player.State == jMusicPlayerState.Playing)
                                                  {
                                                      if (player.mainPlayer.CurrentState == MediaElementState.Playing)
                                                      {
                                                          if(!player.IsSeekingStarted)
                                                          if (!player.IsSeeking)
                                                          {
                                                              double k = player.mainPlayer.Position.TotalSeconds/
                                                                         player.mainPlayer.NaturalDuration.TimeSpan.
                                                                             TotalSeconds;
                                                              player.curPosition.Value = k*
                                                                                         (player.curPosition.Maximum -
                                                                                          player.curPosition.Minimum);
                                                          }
                                                          else
                                                          {
                                                              player.IsSeeking = false;
                                                          }
                                                          player.curTime.Text = player.mainPlayer.Position.ToHHMMSS();
                                                      }
                                                  }
                                              });
            
        }

        private jMusicPlayerState _state;
        private jMusicPlayerState State
        {
            get { return _state; }
            set
            {
                switch (value)
                {
                        case jMusicPlayerState.NoMedia:
                        playStop.Source =
                            new BitmapImage(new Uri("../Resources/play_Disabled.png", UriKind.RelativeOrAbsolute));
                        curTime.Text = "0:00";
                        overAllTime.Text = "0:00";
                        
                        curPosition.IsEnabled = false;
                        playingPanel.Visibility = Visibility.Collapsed;
                        curPosition.Value = 0;
                        break;

                        case jMusicPlayerState.Playing:
                        playStop.Source =
                            new BitmapImage(new Uri("../Resources/play_Pause.png", UriKind.RelativeOrAbsolute));
                        curPosition.IsEnabled = true;
                        overAllTime.Text = mainPlayer.NaturalDuration.TimeSpan.ToHHMMSS();
                        failImage.Visibility = Visibility.Collapsed;
                        ShowPlaying();
                        break;

                        case jMusicPlayerState.Paused:
                        curPosition.IsEnabled = true;
                        playStop.Source =
                            new BitmapImage(new Uri("../Resources/play_Play.png", UriKind.RelativeOrAbsolute));
                        HidePlaying();
                        break;
                }
                         
                _state = value;

            }
        }

        

        public void Play(Uri file)
        {
            if (file != null)
            {
                if (mainPlayer.Source == null || mainPlayer.Source.AbsoluteUri==null || file.AbsoluteUri.ToUpper() != mainPlayer.Source.AbsoluteUri.ToUpper())
                            {
                                ShowPlaying();
                                mainPlayer.Source = file;
                            }
                else
                {
                    if(mainPlayer.CurrentState==MediaElementState.Paused)
                    {
                        mainPlayer.Play();
                        State = jMusicPlayerState.Playing;
                        ShowPlaying();
                    }

                }
            }
        }

        private void media_Ended(object sender, RoutedEventArgs e)
        {
            State = jMusicPlayerState.Paused;
        }

        private void media_Failed(object sender, ExceptionRoutedEventArgs e)
        {
            State = jMusicPlayerState.NoMedia;
            failImage.Visibility = Visibility.Visible;
        }

        private void ShowPlaying()
        {
            playingPanel.Visibility = Visibility.Visible;
            PlayPlaying.Begin();
        }
        private void HidePlaying()
        {
            playingPanel.Visibility = Visibility.Collapsed;
            PlayPlaying.Stop();
        }

        private void playStop_Clicked(object sender, MouseButtonEventArgs e)
        {
            switch (_state)
            {
                    case jMusicPlayerState.Playing:
                     mainPlayer.Pause();
                State = jMusicPlayerState.Paused;
                    break;
                    case jMusicPlayerState.Paused:
                    mainPlayer.Play();
                    State = jMusicPlayerState.Playing;
                    break;
                default:
                    case jMusicPlayerState.NoMedia:
                    break;
            }
        }

        private void opened(object sender, RoutedEventArgs e)
        {
            mainPlayer.Play();
            failImage.Visibility = Visibility.Collapsed;
            State = jMusicPlayerState.Playing;
        }

        public bool IsSeeking = false;
        public bool IsSeekingStarted = false;
        private void seekToMedia(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            if (mainPlayer.CurrentState == MediaElementState.Paused || mainPlayer.CurrentState == MediaElementState.Playing)
            {
                mainPlayer.Position =
                    TimeSpan.FromSeconds(curPosition.Value / ((curPosition.Maximum -
                                                                                      curPosition.Minimum)) * mainPlayer.NaturalDuration.TimeSpan.TotalSeconds);
                IsSeeking = true;
                IsSeekingStarted = false;
            }
        }

        private void seekToMediaStarted(object sender, MouseButtonEventArgs e)
        {
            IsSeekingStarted = true;
        }
    }
}
